// pages/bookDetail/bookDetail.js
var businessLogic = require('../../../utils/NetWork/Model/BookbusinessLogic.js')
var common = require('../../../utils/common.js')

var isCollected = new Array()
Page({

  /**
   * 页面的初始数据
   */
  data: {

  },
  
  /**
   * 生命周期函数--监听页面加载
   */
    onLoad: function (options) {
        var bookid = common.setStorageSync("bookid", options.id)
        let c=common.Getorgid();
        this.setData({
            orgid: c.orgid,
            userid: c.userid,
            bookid: common.getStorageSync("bookid")
        });
        this.bookInformation(bookid);
        this.CheckBook(bookid);
    }, 

        /**
         * 生命周期函数--监听页面初次渲染完成
         */
            onReady: function () {
            
            },

        /**
         * 生命周期函数--监听页面显示
         */
        onShow: function () {
            
        },

        /**
         * 生命周期函数--监听页面隐藏
         */
        onHide: function () {
        
        },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {
        isCollected.splice(0, isCollected.length)
        // bookid:''
    },

        /**
         * 页面相关事件处理函数--监听用户下拉动作
         */
        onPullDownRefresh: function () {
        
        },

        /**
         * 页面上拉触底事件的处理函数
         */
        onReachBottom: function () {
        
        },

        /**
         * 用户点击右上角分享
         */
        onShareAppMessage: function () {
        
        },

    //4.2.1
        bookInformation: function (bookid) {
            let params = { "orgid": this.data.orgid, userid: this.data.userid, id: this.data.bookid }
        businessLogic.bookInformation(params, (res) => {
            console.log(params)
            console.log(res);
            this.setData({
                bookInformation: res
            });
        });
    }, 
    //4.2.3 查询书本是否被收藏
        CheckBook: function (bookid) {
            let params = { orgid: this.data.orgid, userid: this.data.userid, searchField: "peopleid, bookid", searchValue: this.data.userid + "," + this.data.bookid }
            businessLogic.CheckBook(params, (res) => {
                console.log(params)
                console.log(res);

                if (res){
                    isCollected.push(res);
                    this.setData({ checkCollection: isCollected[0].iscollect})
                }else{
                    this.setData({ checkCollection: 0})
                }
            });
        },
        Collection: function () {
                if (!isCollected[0]) { 
                    this.firstCollect() 
                } else if (isCollected[0].iscollect == "0") {
                    var collectid = isCollected[0].id
                    this.recollect(collectid)   
                } else { 
                    var collectid = isCollected[0].id
                    this.cancelCollect(collectid)   
                }
        },

        recollect: function (collectid){
        // 4.2.2 收藏或取消收藏书本
            var InfoJson = "{orgid:'" + this.data.orgid + "',userid:'" + this.data.userid + "',peopleid:'" + this.data.userid + "',bookid:'" + this.data.bookid + "',id:'" + collectid + "',iscollect:1}"
            let params = { "orgid": this.data.orgid, "userid": this.data.userid, "InfoJson": InfoJson }
            businessLogic.bookUpdateFrontInfo(params, (res) => {
                this.setData({ checkCollection: 1  })
                isCollected[0].iscollect = "1"
            });
        },

        firstCollect: function (collectid) {
        //首次收藏
            let params = { "orgid": this.data.orgid, "userid": this.data.userid, "InfoJson": "{orgid:'" + this.data.orgid + "',userid:'" + this.data.userid + "',peopleid:'" + this.data.userid + "',bookid:'" + this.data.bookid + "',id:'',iscollect:1}" }
            businessLogic.bookUpdateFrontInfo(params, (res) => {
                console.log(params)
                console.log(res);
                this.setData({ checkCollection: 1     })
                isCollected.push(res)
            });
        },

        cancelCollect: function (collectid) {
        //取消收藏
            let params = { "orgid": this.data.orgid, "userid": this.data.userid, "InfoJson": "{orgid:'" + this.data.orgid + "',userid:'" + this.data.userid + "',peopleid:'" + this.data.userid + "',bookid:'"+ this.data.bookid + "',id:'"+ collectid + "',iscollect:0}" }
            businessLogic.bookUpdateFrontInfo(params, (res) => {
                this.setData({ checkCollection: 0    })
                isCollected[0].iscollect = '0'
            });
        },
    //阅读
    reading: function (e) {
       var bookid = e.currentTarget.dataset.id
        var url = '../bookexe/bookexe?id=' + bookid;
        wx.navigateTo({url: url}) 
     

    },

})